<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller {
	public function _initialize(){
		layout(false);
//        layout(true);
	}
	
	//用户注册
	public function regPage(){
		$ruleResults=M('Rule')->select();
		$rules=array();
		foreach($ruleResults as $rule) {
			$rules[$rule['name']] = $rule['value'];
			$this->assign('rules',$this->rules);
		}
		$this->assign('reg_level',$rules['reg_level']);
		$this->display();
	}
	
	//判断游戏UID是否存在
	public function checkUserId(){
		$user_id=I('user_id');
		$url=C('API_URL').'/?act=checkUserRegisterAgent&uid='.$user_id;
		$curl_res=get_curl($url);
		if($curl_res['data'] == true) {
		    $this->ajaxReturn('exist');
        }else{
		    $this->ajaxReturn('unexist');
        }
	}
	public function checkedUidState() {
	    $user_id=I('user_id');
        $url=C('API_URL').'/?act=hasUser&uid='.$user_id;
        $curl_res=get_curl($url);

        if($curl_res['data'] == true) {
            $this->ajaxReturn('exist');
        }else{
            $this->ajaxReturn('unexist');
        }
    }

//	判断注册的代理是否已在数据库
    public function checkUserIdSql(){
	    $user_id = I("user_id");
	    $uOb = M('User');
	    $result = $uOb->where('user_id='.$user_id)->find();
	    if($result) {
	        $this->ajaxReturn('uexist');
        }
    }
	
	//判断用户是否已存在
	public function checkTel(){
		$uOb=M('User');
		$tel=I('tel');
        $result = $uOb->where('tel='.$tel)->find();
        if($result) {
            $this->ajaxReturn('texist');
        }
	}
	//用户注册数据保存
	public function regSave(){
		$uOb=M('User');
		$data=I('post.');
		$rOb = M('Rule');
		$rerole = $rOb->where('name = "agent_level"')->find();
        $maxlevel = $rerole['value'];
		$exist=$uOb->where(array('tel'=>$data['tel'],'user_id'=>$data['user_id']))->find();
		if($exist) {
			$this->ajaxReturn('exist');
		}

		$url=C('API_URL').'/?act=checkUserRegisterAgent&uid='.I('user_id');
		$curl_res=get_curl($url);
		if($curl_res['data'] == false) {
			$this->ajaxReturn('unexist');
		}

		$results = $rOb->where('name="reg_level"')->find();
        $reg_level = $results['value'];

        if($reg_level == 1) {
            if($data['refer']) {
                $refer_user = $uOb->field('role')->where(array('user_id'=>$data['refer']))->find();
                if($refer_user) {
                    if($refer_user['role'] >= $maxlevel) {
                        $this->ajaxReturn('noRefer');
                    }else{
                        $data['role'] = $refer_user['role'] + 1;
                    }
                }else{
                    $this->ajaxReturn('noRefer');
                }
            }else{
                $data['role'] = 1;
            }
        }else if($reg_level == 3) {
            if($data['refer']) {
                $refer_user = $uOb->field('role')->where(array('user_id'=>$data['refer']))->find();
                if($refer_user) {
                    if($refer_user['role'] >= $maxlevel) {
                        $this->ajaxReturn('noRefer');
                    }else{
                        $data['role'] = $refer_user['role'] + 1;
                    }
                }else{
                    $this->ajaxReturn('noRefer');
                }
            }else{
                $data['role'] = 3;
            }
        }


        $data['create_time'] = time();
//      $data['expirate'] = time()+2592000;
        $data['password']=md5($data['password']);
		
        trace($data, "regPage");
        $result = $uOb->add($data);
        if ($result) {
            $this->ajaxReturn('success');
        }else{
            $this->ajaxReturn('error');
        }
	}
	
	//用户详细信息
	public function infoSave(){
		$this->display();
	}
	
	//用户详细信息的保存
	public function saveInfo(){
		$data=I('post.');
		$uOb=M('User');
		$result=$uOb->where('tel='.$data['tel'])->save(array('bank_card'=>$data['bank_card'],'bank_name'=>$data['bank_name'],'name'=>$data['name'],'card_id'=>$data['card_id']));
		if($result){
			$this->ajaxReturn('success');
		}else{
			$this->ajaxReturn('failure');
		}
	}
	
	//登录页面
	public function login(){
		$this->display();
	}


	//登录验证
	function loginCheck(){
		$telId=I('telId');
		$password=md5(I('password'));
//		$time=I('time');
		$uOb=M('User');

		$condition['tel']=$telId;
		$condition['user_id']=$telId;
		$condition['_logic']='OR';

		$user=$uOb->where($condition)->find();
		if($user) {
			if($user['password'] != $password) {
				$this->ajaxReturn("passwordIncorrect");
			}

			if($user['status'] == 0) {
				$this->ajaxReturn('unactive');
			}

//			if($time>$user['expirate']) {
//				$this->ajaxReturn('danger');
//			}

			if($user['state'] == 0) {
				$this->ajaxReturn('titles');
			}

			session('user',$user);
			session('adminTel',$telId);
			session('user_id',$user['user_id']);
			session('role',$user['role']);
			$this->ajaxReturn('success');

		}else{
			$this->ajaxReturn('notExist');
		}
		
	}
//	public function yzm(){
//	    $Verify = new \Think\Verify();
//	    $Verify->fontSize = 26;
//	    $Verify->length = 4;
//	    $Verify->useCurve = false;
//	    $Verify->useNoise = false;
//	    $Verify->seKey = true;
//	    $Verify->entry();
//    }
	//退出登录
	function out(){
        session('adminname',null);
        session('user_id',null);
		session('role',null);
		header("location:".U('Admin/Login/login'));
	}
}
?>